<?php

/**
 * SimplifyPHP Framework
 *
 * This file is part of SimplifyPHP Framework.
 *
 * SimplifyPHP Framework is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * SimplifyPHP Framework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>
 */

/**
 * Abstract Data Access Object
 *
 * @author "Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>"
 *
 */
abstract class DAO
{

  /**
   *
   * @var array
   */
  private static $instances = array();

  /**
   *
   * @var DomainDefinition
   */
  protected $config;

  /**
   * Parameters
   *
   * @var array
   */
  protected $params;

  /**
   * Constructor.
   *
   * @param array $params
   * @return void
   */
  protected function __construct($params)
  {
    $this->config = new DomainDefinition();
    $this->params = SimpleDictionary::parseFrom($params);
  }

  /**
   * Destructor.
   *
   * @return void
   */
  public function __destruct()
  {
    $this->disconnect();
  }

  /**
   * Get the DomainDefinition for this DAO.
   *
   * @return DomainDefinition
   */
  public function getConfig()
  {
    return $this->config;
  }

  /**
   * Add an entity definition to the domain.
   *
   * @param string $name
   * @param EnitityDefinition|array $definition
   * @return DAO
   */
  public function addEntity($name, $definition)
  {
    if (! is_a($definition, 'EntityDefinition')) {
      $definition = new EntityDefinition($definition);
    }

    $this->config->addEntity($name, $definition);

    return $this;
  }

  /**
   * Get an entity definition from the domain.
   *
   * @param string $name
   * @return EntityDefinition
   */
  public function getEntity($name)
  {
    if (! $this->config->hasEntity($name)) {
      throw new Exception("Entity not found: <b>$name</b>");
    }

    return $this->config->getEntity($name);
  }

  /**
   * Create or update an entity.
   *
   * @param Entity $o the entity
   * @return void
   */
  public function save(Entity $o)
  {
    if ($o->isNew()) {
      $this->create($o);
    }
    elseif ($o->isDirty()) {
      $this->update($o);
    }
  }

  /**
   * Insert a new row in the repository
   *
   * @param Entity $o
   * @return void
   */
  protected function create(Entity $o)
  {
    $def = $o->getDefinition();

    $pk = $def->getPrimaryKey();

    $o->$pk = $this->lastInsertID();

    $o->commit();
  }

  /**
   * Update a row in the repository
   *
   * @param Entity $o
   * @return void
   */
  protected function update(Entity $o)
  {
    $o->commit();
  }

  /**
   * Get last insert ID
   *
   * @return mixed
   */
  abstract public function lastInsertID();

  /**
   * Connect to the repository.
   *
   * @return DAO
   */
  abstract public function connect();

  /**
   * Disconnect from the repository.
   *
   * @return DAO
   */
  abstract public function disconnect();

  /**
   * Get the connection to the repository.
   *
   * @return mixed
   */
  abstract public function db();

  /**
   * Get the instance for the DAO
   *
   * @param string $id
   * @param string $engine
   * @param array $params
   * @return DAO
   */
  public static function getInstance($id = 'default', $engine = 'MDB2', $params = null)
  {
    if (empty(self::$instances[$id])) {
      if (! Simplify::getConfig()->has('database:' . $id)) {
        throw new Exception("Database configuration for <b>$id</b> not found");
      }

      $class = $engine . '_DAO';
      self::$instances[$id] = new $class(Simplify::getConfig()->get('database:' . $id)->getAll());
    }

    return self::$instances[$id];
  }

}

?>